package com.jtny.nytb.config.unifiedIdentification.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.jtny.component.entity.PageData;
import com.jtny.component.entity.WebResponse;
import com.jtny.component.enums.GenericStatusEnum;
import com.jtny.component.util.BeanUtils;
import com.jtny.nytb.config.excel.biz.ImportExcelBiz;
import com.jtny.nytb.config.excel.util.EasyExcelUtils;
import com.jtny.nytb.config.unifiedIdentification.dto.UnifiedIdentifierRequestDTO;
import com.jtny.nytb.config.unifiedIdentification.dto.UnifiedIdentifierResponseDTO;
import com.jtny.nytb.config.unifiedIdentification.service.UnifiedIdentifier;
import com.jtny.nytb.config.unifiedIdentification.vo.UnifiedIdentifierQueryParamVO;
import com.jtny.nytb.config.unifiedIdentification.vo.UnifiedIdentifierVO;
import com.jtny.nytb.config.unifiedIdentification.vo.UnifiedIdentifierFormVO;
import com.jtny.nytb.config.unifiedIdentification.vo.ExcelUnifiedIdentifierVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.jtny.nytb.valid.param.Update;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@AllArgsConstructor
@Api(value = "数据统一标识符字典", tags = "数据统一标识符字典")
@ApiSort(202310)
@RequestMapping("unified-dict")
public class UnifiedIdentifierController {

    private UnifiedIdentifier unifiedIdentifierService;
    private ImportExcelBiz importExcelBiz;

    /**
     * 数据统一标识符字典 详情
     *
     * @param id 报表标识符 id
     * @return {@link WebResponse}<{@link UnifiedIdentifierVO}>
     */
    @GetMapping("detail/{id}")
    @ApiOperation("详情")
    @ApiOperationSupport(order = 1)
    public WebResponse<UnifiedIdentifierVO> detail(@PathVariable("id") Long id) {
        UnifiedIdentifierResponseDTO unifiedIdentifier = unifiedIdentifierService.detail(id);
        return WebResponse.success(BeanUtils.copyToObject(unifiedIdentifier, UnifiedIdentifierVO.class));
    }

    /**
     * 数据统一标识符字典 分页
     *
     * @param unifiedIdentifierQueryParamVO 数据统一标识符查询参数 VO
     * @return {@link WebResponse}<{@link PageData}<{@link UnifiedIdentifierVO}>>
     */
    @PostMapping("page")
    @ApiOperation("分页")
    @ApiOperationSupport(order = 2)
    public WebResponse<PageData<UnifiedIdentifierVO>> page(@RequestBody UnifiedIdentifierQueryParamVO unifiedIdentifierQueryParamVO) {
        PageData<UnifiedIdentifierResponseDTO> pageData = unifiedIdentifierService.page(unifiedIdentifierQueryParamVO);
        return WebResponse.success(pageData.transformData(UnifiedIdentifierVO.class));
    }


    /**
     * 数据统一标识符字典 保存
     *
     * @param unifiedIdentifierFormVO 数据统一标识符表单 VO
     * @return {@link WebResponse}<{@link String}>
     */
    @PostMapping("save")
    @ApiOperation("保存")
    @ApiOperationSupport(order = 3)
    public WebResponse<?> save(@RequestBody @Validated UnifiedIdentifierFormVO unifiedIdentifierFormVO) {
        unifiedIdentifierService.save(BeanUtils.copyToObject(unifiedIdentifierFormVO, UnifiedIdentifierRequestDTO.class));
        return WebResponse.success();
    }

    /**
     * 数据统一标识符字典 更新
     *
     * @param unifiedIdentifierFormVO 数据统一标识符表单 VO
     * @return {@link WebResponse}<{@link String}>
     */
    @PutMapping("update")
    @ApiOperation("更新")
    @ApiOperationSupport(order = 4)
    public WebResponse<?> update(@RequestBody @Validated(Update.class) UnifiedIdentifierFormVO unifiedIdentifierFormVO) {
        UnifiedIdentifierRequestDTO unifiedIdentifierRequestDTO = BeanUtils.copyToObject(unifiedIdentifierFormVO, UnifiedIdentifierRequestDTO.class);
        unifiedIdentifierService.update(unifiedIdentifierRequestDTO);
        return WebResponse.success();
    }

    /**
     * 数据统一标识符字典 禁用/启用
     *
     * @param id 数据统一标识符 id
     * @return {@link WebResponse}<{@link String}>
     */
    @PutMapping("status/{id}/{status}")
    @ApiOperation("禁用/启用")
    @ApiOperationSupport(order = 5)
    public WebResponse<?> status(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        UnifiedIdentifierRequestDTO unifiedIdentifier = new UnifiedIdentifierRequestDTO();
        unifiedIdentifier.setId(id);
        unifiedIdentifier.setStatus(status);
        unifiedIdentifierService.update(unifiedIdentifier);
        return WebResponse.success();
    }


    /**
     * 数据统一标识符字典 删除
     *
     * @param id 数据统一标识符 id
     * @return {@link WebResponse}<{@link String}>
     */
    @DeleteMapping("delete/{id}")
    @ApiOperation("删除")
    @ApiOperationSupport(order = 6)
    public WebResponse<?> delete(@PathVariable("id") Long id) {
        UnifiedIdentifierRequestDTO unifiedIdentifier = new UnifiedIdentifierRequestDTO();
        unifiedIdentifier.setId(id);
        unifiedIdentifier.setStatus(GenericStatusEnum.DELETED.getValue());
        unifiedIdentifierService.update(unifiedIdentifier);
        return WebResponse.success();
    }

    /**
     * 获取导入模板
     * @param response 响应
     */
    @PostMapping("/export-template")
    @ApiOperation(value = "获取导入模板")
    public void exportTemplate(HttpServletResponse response) {
        EasyExcelUtils.exportExcel(response, "统一标识符导入模版", null, ExcelUnifiedIdentifierVO.class);
    }

    @ApiOperation("导入统一标识符")
    @ApiImplicitParam(name = "file", value = "统一标识符excel文件", required = true, paramType = "query", dataType = "MultipartFile")
    @ApiOperationSupport(order = 9)
    @PostMapping("/import")
//    @OperationLog(value = "导入计量单位分类")
    @ResponseBody
    public WebResponse<?> importUnifiedIdentifier(@RequestParam(value = "file") MultipartFile file) {
        List<ExcelUnifiedIdentifierVO> list = EasyExcelUtils.importExcel(file, ExcelUnifiedIdentifierVO.class);
        unifiedIdentifierService.importUnifiedIdentifierExcel(list);
        return WebResponse.success();
    }

    @ApiOperation("导出统一标识符")
    @ApiOperationSupport(order = 10)
    @PostMapping("/export")
//    @OperationLog(value = "导出统一标识符")
    @ApiImplicitParam(name = "idList", value = "id列表", required = true, paramType = "body", dataType = "List<Long>")
    @ResponseBody
    public void exportUnifiedIdentifier(HttpServletResponse response, @RequestBody List<Long> idList) {
        UnifiedIdentifierQueryParamVO paramVO = new UnifiedIdentifierQueryParamVO();
        paramVO.setIdList(idList);
        paramVO.setPageSize(10000);
        PageData<UnifiedIdentifierResponseDTO> page = unifiedIdentifierService.page(paramVO);
        List<ExcelUnifiedIdentifierVO> toList = BeanUtils.copyToList(page.getData(), ExcelUnifiedIdentifierVO.class);
        EasyExcelUtils.exportExcel(response, "数据字典", toList, ExcelUnifiedIdentifierVO.class);
    }

}
